﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.pictureinfo.xml;
using rock.UI.pictureinfo.subview;

namespace rock.UI.picturexml
{
    public partial class TestDrow : UserControl
    {
        public TestDrow()
        {
            InitializeComponent();
        }

        private void panel1Parent_Paint(object sender, PaintEventArgs e)
        {
           //Root root =  XmlUtil.parse(Application.StartupPath + @"\picture_xml\food.xml");
           
        }

        private void TestDrow_Load(object sender, EventArgs e)
        {
            Root root = XmlUtil.parse(@"E:\VS2010\SVN\RentalHousingManager\rockpear\RentalHousingManager2\RentalHousingManager2\UI\picturexml\picture_xml\food.xml");
            foreach (Row row in root.rows) // Loop through List with foreach
            {
                //row.name 
                drowRow(row);

            }

            this.panel1Parent.Width = maxW;
            this.panel1Parent.Height = maxH;

            this.panel1Parent.Refresh();

        }

        int top = 0;
        int w = 20;
        int h = 40;

        int maxW = 0;
        int maxH = 0;

        public void drowRow(Row row)
        {
            int left = 30;
            top = (row.order - 1) * (h + 10) * 2;


            Label label = new Label();
            label.Text = row.name;
            label.Top = top;
            label.Left = 0;
            label.Width = 20;
            label.Height = 30;
            this.panel1Parent.Controls.Add(label);
            
            
            

           

            foreach (Col col in row.cols) // Loop through List with foreach
            {
                String lastLayat = "";
             
                String[] parts = col.parts;
                String[] ondouble = col.ondouble;

                for (int i = col.start; i <= col.end; i++) 
                {
                    if (col.layout.Equals("vertical"))
                    {
                        lastLayat = "vertical";
                        //top = top ; 

                        int hTemp = h;
                        if (col.heightmodel != null && col.heightmodel.Equals("half"))
                        {
                            hTemp =   h /2;
                        }

                        int wTemp = h;
                        if (col.widthmodel != null && col.widthmodel.Equals("double"))
                        {
                            wTemp = h * 2;
                        }

                        UserControlHouse ucuA = this.create(row.xiaoqu, col.dong, i, "", top, left, wTemp, hTemp);
                        this.panel1Parent.Controls.Add(ucuA);
                        top += hTemp * 1  ;
                         
                        //ucuA.inits();
                    }
                    else
                    {
                        lastLayat = "horizontal";
                        if (col.style.Equals("double"))
                        {
                            if (parts != null && parts.Count() > 0)
                            {
                                //是否有通道
                                for (int j = 0; j < parts.Count(); j++)
                                {
                                    if (((i - 1) + "").Equals(parts[j]))
                                    {
                                        //分隔
                                        left += 10;
                                    }
                                }
                            }
                            //是否是通间
                            bool needAB = true;
                            if (parts != null && ondouble.Count() > 0)
                            {
                               
                                for (int j = 0; j < ondouble.Count(); j++)
                                {
                                    if ((i + "").Equals(ondouble[j]))
                                    {
                                        //分隔
                                        needAB = false;
                                    }
                                }
                            }
                            if (needAB)
                            {
                                int topA = top + col.top;
                                left += w * 1;
                                UserControlHouse ucuA = this.create(row.xiaoqu, col.dong, i, "B", topA, left+col.left, w, h);
                                this.panel1Parent.Controls.Add(ucuA);
                               // ucuA.inits();

                                int topB = top + h * 1 + col.top;
                                UserControlHouse ucuB = this.create(row.xiaoqu, col.dong, i, "A", topB, left + col.left, w, h);
                                this.panel1Parent.Controls.Add(ucuB);
                               // ucuB.inits();
                            }
                            else {
                                 
                                left += w * 1;
                                UserControlHouse ucuA = this.create(row.xiaoqu, col.dong, i, "", top, left + col.left, w, h * 2);
                                this.panel1Parent.Controls.Add(ucuA);
                               // ucuA.inits();
                            }
                            
                        }
                        else
                        {   //8号单排的向下沉一个房高
                            int topTemp = top; 
                            if(col.top == -1){
                                topTemp = top +col.top+  h * 1;
                            }
                            //top = top ; 
                            left += w*1  ;
                            if (parts != null && parts.Count() > 0) { 
                                //是否有通道
                                for (int j = 0; j < parts.Count(); j++)
                                {
                                    if (((i - 1) + "").Equals(parts[j]))
                                    {
                                        //分隔
                                        left += 10;
                                    }
                                }
                            }
                            int hTemp = h;
                            if (col.heightmodel != null && col.heightmodel.Equals("double"))
                            {
                                hTemp = 2*h;
                            }

                            UserControlHouse ucuA = this.create(row.xiaoqu, col.dong, i, "", topTemp, left + col.left, w, hTemp);
                            this.panel1Parent.Controls.Add(ucuA);
                            if (maxH < topTemp) {
                                maxH = topTemp;
                            }

                            if (maxW < left + col.left)
                            {
                                maxW = left + col.left;
                            }

                           // ucuA.inits();

                        }
                    }
                }
                if (lastLayat.Equals("vertical"))
                {
                    left += w * 1;
                    top = (top - h * 2);// +(lastHeight / 2);
                }
               
               
            }
        }

        //F1  -  4   106   A
        public UserControlHouse create(String xiaoqu, String dong, int seqid, String ab, int top, int left, int w, int h)
        {
            UserControlHouse u = new UserControlHouse();
            
            u._AB = ab;
            u._ADONG = dong;
            u._ASEQID = seqid;
            u._AXIAOQU = xiaoqu;

            u._HOUSE_TEXT = ""+seqid;
            u.Top = top;
            u.Left = left;
            u.Width = w;
            u.Height = h;
            return u; 
        }
        
    }
}
